package com.libweb.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.libweb.model.dao.MemberDao;
import com.libweb.model.dto.Member;

@WebServlet("/member/view.action")
public class MemberViewServlet extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		
		//1. 요청 정보에서 조회할 아이디를 읽고 변수에 저장
		//   (아이디가 없으면, 목록으로 이동)
		String id = req.getParameter("memberid");
		if (id == null || id.length() == 0) {
			resp.sendRedirect("/libweb/member/list.action");
			return;
		}
		
		//2. 1에서 읽은 아이디로 데이터베이스에서 조회
		//   (조회된 데이터가 없으면 목록으로 이동)
		MemberDao dao = new MemberDao();
		Member member = dao.getMemberById(id);
		if (member == null) {
			resp.sendRedirect("/libweb/member/list.action");
			return;
		}
		
		
		req.setAttribute("member", member);
		
		//3-2. 응답 컨텐츠 생성 (JSP 이용해서 처리 - jsp page로 forward)
		RequestDispatcher dispatcher = 
			req.getRequestDispatcher("/WEB-INF/views/member/view.jsp");

		dispatcher.forward(req, resp);
		
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
		throws ServletException ,IOException {
		
		doGet(req, resp);
	};

}








